﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using static CoreShop.Library.GlobalEnumVars;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;
using NPOI.SS.Formula.Functions;

namespace CoreShop.App
{
    public partial class frmSetReduce : UIForm
    {
        public corecmsfoodorderingmaster orderingMaster = null;
        public List<corecmsfoodorderingtag> orderingTags = null;
        public int discountType = 1;//1优惠，2实收
        public frmSetReduce(int _type)
        {
            discountType = _type;
            InitializeComponent();
            txtReduceAmout.TextBox.GotFocus += txtReduceAmout_GotFocus;
            txtAllAmout.TextBox.GotFocus += txtAllAmout_GotFocus;
        }

        private void txtReduceAmout_GotFocus(object sender, System.EventArgs e)
        {
            //numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 2);
        }

        private void txtAllAmout_GotFocus(object sender, System.EventArgs e)
        {
            //numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 2);
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {
            mbtDiscountTag.Clear();
            mbtDiscountTag.AutoWidth = true;
            var tagList = new corecmsfoodtagServices().QueryAsync((int)GlobalEnumVars.FoodTag.PriceTag);
            foreach (var item in tagList)
            {
                mbtDiscountTag.Add(item.content, item.id);
            }
        }        

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                double fInput;
                if (!double.TryParse(txtReduceAmout.Text, out fInput))
                {
                    UIMessageBox.Show("请输入有效的数字。", "系统出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                    return;
                }
                numPad_OKButtonClick(null, null,0,0);
            }
        }
        
        private void btnExit_Click(object sender, EventArgs e)
        {

        }

        private void txtSendKey_Validating(object sender, CancelEventArgs e)
        {
            double fInput;
            if (!double.TryParse(txtReduceAmout.Text, out fInput))
            {
                MessageBox.Show("请输入有效的数字！");
                e.Cancel = true;
            }
        }

        private void frmInputTxt_Shown(object sender, EventArgs e)
        {
            decimal dAllAmount = orderingMaster.sumAllAmount1 - orderingMaster.sumFloorAmount;
            lblAmount.Text = dAllAmount.ToString("0.0#");
            txtAllAmout.Text = orderingMaster.sumAllAmount.ToString("0.0##");
            txtReduceAmout.Text = (dAllAmount - orderingMaster.sumAllAmount).ToString("0.0##");            
            if (discountType == 1)
            {
                txtReduceAmout.Focus();
                txtReduceAmout.SelectAll();
            }
            else
            {
                txtAllAmout.Focus();
                txtAllAmout.SelectAll();
            }
        }        

        private void txtAllAmout_Enter(object sender, EventArgs e)
        {
            discountType = 2;
            numPad.SetTextBox(sender as UITextBox, UITextBox.UIEditType.Double, 2);
            if (CommFunc.ConvertToDecimal((sender as UITextBox).Text) == 0)
                (sender as UITextBox).Text = null;
        }

        private void txtReduceAmout_Enter(object sender, EventArgs e)
        {
            discountType = 1;
            numPad.SetTextBox(sender as UITextBox, UITextBox.UIEditType.Double, 2);
            if (CommFunc.ConvertToDecimal((sender as UITextBox).Text) == 0)
               (sender as UITextBox).Text = null;
        }

        private void txtAllAmout_TextChanged(object sender, EventArgs e)
        {
            txtAllAmout.ShowButton = !string.IsNullOrEmpty(this.txtAllAmout.Text);
            if (discountType == 2)
            {
                txtReduceAmout.Text = (CommFunc.ConvertToDecimal(lblAmount.Text) - CommFunc.ConvertToDecimal(txtAllAmout.Text)).ToString();
            }
        }

        private void txtReduceAmout_TextChanged(object sender, EventArgs e)
        {
            txtReduceAmout.ShowButton = !string.IsNullOrEmpty(this.txtReduceAmout.Text);
            if (discountType == 1)
            {
                txtAllAmout.Text = (CommFunc.ConvertToDecimal(lblAmount.Text) - CommFunc.ConvertToDecimal(txtReduceAmout.Text)).ToString();
            }
        }

        public string desc = "";
        private void mbtDiscountTag_SelectedChanged(object sender, EventArgs e)
        {
            UIMultiButtons button = (UIMultiButtons)sender;
            UIButton btn = button.SelectedBtn;

            if (btn != null)
            {
                string btnTxt = btn.Text;
                bool btnSelect = btn.Selected;
                if (btnSelect)
                    desc = btnTxt;
                else
                    desc = "";
            }
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            decimal realAmount;
            string sInput = txtReduceAmout.Text;
            if (string.IsNullOrEmpty(sInput)) sInput = "0";
            if (!decimal.TryParse(sInput, out realAmount))
            {
                UIMessageBox.Show("请输入合法的优惠金额。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            sInput = txtAllAmout.Text;
            if (string.IsNullOrEmpty(sInput)) sInput = "0";
            if (!decimal.TryParse(sInput, out realAmount))
            {
                UIMessageBox.Show("请输入合法的实收金额。", "出错", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }

            decimal freeAmount = orderingMaster.sumAllAmount1 - orderingMaster.sumFloorAmount - realAmount;

            if (!ProcessUtil.GetUserRole(GlobalEnumVars.UserRoleType.maxReduceAmount, freeAmount)) return;

            orderingMaster.sumAllAmount = realAmount;
            var bl = new OrderingUtil().SetOrderingFreeAmount(orderingMaster, freeAmount);

            var buttons = this.GetControls<UIMultiButtons>(true);
            foreach (UIMultiButtons button in buttons)
            {
                long[] taskIds = button.SelectedButtonsValues;
                var orderingTag = new corecmsfoodorderingtag();
                orderingTag.detailId = 0;
                orderingTag.orderId = orderingMaster.orderId;
                orderingTag.tagType = (int)FoodTag.PriceTag;
                orderingTag.tagContent = desc;
                orderingTags.Add(orderingTag);
            }
            new corecmsfoodorderingtagServices().InsertRange(orderingTags);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtReduceAmout_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                string str = txtReduceAmout.Text;
                int idx = txtReduceAmout.SelectionStart;
                str = str.Insert(idx, ".");
                txtReduceAmout.Text = str;
                txtReduceAmout.SelectionStart = idx + 1;
                txtReduceAmout.Focus();
                e.Handled = true;
            }
        }

        private void txtAllAmout_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                string str = txtAllAmout.Text;
                int idx = txtAllAmout.SelectionStart;
                str = str.Insert(idx, ".");
                txtAllAmout.Text = str;
                txtAllAmout.SelectionStart = idx + 1;
                txtAllAmout.Focus();
                e.Handled = true;
            }
        }

        private void txtReduceAmout_Leave(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty((sender as UITextBox).Text)) (sender as UITextBox).Text = "0";
        }

        private void txtReduceAmout_ButtonClick(object sender, EventArgs e)
        {
            txtReduceAmout.Text = null;
            txtReduceAmout.Focus();
        }

        private void txtAllAmout_ButtonClick(object sender, EventArgs e)
        {
            txtAllAmout.Text = null;
            txtAllAmout.Focus();
        }
    }
}
